﻿package mortal.game.scene3D.ai.singleAIs
{
    import Message.BroadCast.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.fight.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.systemSetting.*;

    public class TabKeyAI extends Object
    {
        private var _rang1:Rectangle;
        private var _rang2:Rectangle;
        private var _lastTime:int;
        private const egnorTime:int = 4000;
        private var _lastDic:Dictionary;
        private var _au:Vector.<SEntityId>;
        private var _ap:Vector.<SEntityId>;
        private var _am:Vector.<SEntityId>;
        private var _myPoint:Point;
        private var time1:int = 0;
        private var time2:int = 0;
        private static var _instance:TabKeyAI;

        public function TabKeyAI()
        {
            this._rang1 = new Rectangle(0, 0, 400, 400);
            this._rang2 = SceneRange.display;
            this._lastDic = new Dictionary();
            this._myPoint = new Point();
            return;
        }// end function

        public function work() : void
        {
            var _loc_4:IEntity = null;
            var _loc_5:Array = null;
            var _loc_1:* = getTimer();
            this.resetLastDic(_loc_1);
            var _loc_2:* = ThingUtil.entityUtil.entitysMap.getEntityByRangle(this._rang2);
            _loc_2 = this.getCanTabs(_loc_2);
            var _loc_3:* = this.getNotTabs(_loc_2);
            if (_loc_3.length > 0)
            {
                this._au = FightEffectUtil.attackMeUserList;
                this._ap = FightEffectUtil.attackMePetList;
                this._am = FightEffectUtil.attackMeMonsterList;
                this._myPoint.x = RolePlayer.instance.stagePointX;
                this._myPoint.y = RolePlayer.instance.stagePointY;
                _loc_3.sort(this.sortNotTabs);
                _loc_4 = _loc_3[0];
            }
            else
            {
                _loc_5 = _loc_2;
                _loc_5.sort(this.sortTabed);
                _loc_4 = _loc_5[0];
            }
            if (_loc_4 == RolePlayer.instance)
            {
                return;
            }
            ThingUtil.selectEntity = _loc_4;
            if (_loc_4 != null)
            {
                this._lastDic[_loc_4.entityID] = _loc_1;
            }
            return;
        }// end function

        private function sortNotTabs(param1:IEntity, param2:IEntity) : int
        {
            var _loc_3:* = this.isAttakMe(param1) ? (100) : (0);
            var _loc_4:* = this.isAttakMe(param2) ? (100) : (0);
            _loc_3 = _loc_3 + (this.calDistaneMe(param1) <= 400 ? (10) : (0));
            _loc_4 = _loc_4 + (this.calDistaneMe(param2) <= 400 ? (10) : (0));
            _loc_3 = _loc_3 + this.calTypeValue(param1);
            _loc_4 = _loc_4 + this.calTypeValue(param2);
            if (param1 >= param2)
            {
                return -1;
            }
            return 1;
        }// end function

        private function calTypeValue(param1:IEntity) : int
        {
            if (param1 is UserPlayer)
            {
                return 5;
            }
            if (param1 is MonsterPlayer)
            {
                return 0;
            }
            return 2;
        }// end function

        private function calDistaneMe(param1:IEntity) : Number
        {
            return GeomUtil.calcDistance(SpritePlayer(param1).stagePointX, SpritePlayer(param1).stagePointY, this._myPoint.x, this._myPoint.y);
        }// end function

        private function isAttakMe(param1:IEntity) : Boolean
        {
            if (this._au.indexOf(param1.entityInfo.entityInfo.entityId) >= 0)
            {
                return true;
            }
            if (this._ap.indexOf(param1.entityInfo.entityInfo.entityId) >= 0)
            {
                return true;
            }
            if (this._am.indexOf(param1.entityInfo.entityInfo.entityId) >= 0)
            {
                return true;
            }
            return false;
        }// end function

        private function sortTabed(param1:IEntity, param2:IEntity) : int
        {
            if (param1 == RolePlayer.instance)
            {
                return 1;
            }
            if (param2 == RolePlayer.instance)
            {
                return -1;
            }
            this.time1 = this._lastDic[param1.entityID];
            this.time2 = this._lastDic[param2.entityID];
            if (this.time1 < this.time2)
            {
                return -1;
            }
            return 1;
        }// end function

        private function getCanTabs(param1:Array) : Array
        {
            var _loc_3:IEntity = null;
            var _loc_4:SEntityInfo = null;
            var _loc_5:MonsterPlayer = null;
            var _loc_2:Array = [];
            for each (_loc_3 in param1)
            {
                
                _loc_4 = _loc_3.entityInfo.entityInfo;
                if ((_loc_4.entityId.type == EEntityType._EEntityTypePlayer || _loc_4.entityId.type == EEntityType._EEntityTypePet) && !_loc_3.entityInfo.isInLayer)
                {
                    continue;
                }
                if (_loc_4.entityId.type == EEntityType._EEntityTypePlayer && SystemSetting.instance.isNotAutoSelectPlayer.bValue || _loc_4.entityId.type == EEntityType._EEntityTypeBoss && SystemSetting.instance.isNotAutoSelectMonster.bValue || _loc_4.entityId.type == EEntityType._EEntityTypePet)
                {
                    continue;
                }
                if (EntityRelationUtil.isFriend(_loc_4))
                {
                    continue;
                }
                if (_loc_4.life <= 0)
                {
                    continue;
                }
                if (_loc_3 is MonsterPlayer)
                {
                    _loc_5 = _loc_3 as MonsterPlayer;
                    if (!BossRule.isBossTypeCanSelect(_loc_5.bossInfo.type))
                    {
                        continue;
                    }
                }
                _loc_2.push(_loc_3);
            }
            return _loc_2;
        }// end function

        private function getNotTabs(param1:Array) : Array
        {
            var _loc_3:IEntity = null;
            var _loc_4:SEntityInfo = null;
            var _loc_2:Array = [];
            if (param1 == null || param1.length == 1)
            {
                return _loc_2;
            }
            for each (_loc_3 in param1)
            {
                
                _loc_4 = _loc_3.entityInfo.entityInfo;
                if (this._lastDic[_loc_3.entityID] != null)
                {
                    continue;
                }
                _loc_2.push(_loc_3);
            }
            return _loc_2;
        }// end function

        private function resetLastDic(param1:int) : void
        {
            var _loc_3:* = undefined;
            var _loc_4:* = undefined;
            var _loc_5:int = 0;
            var _loc_2:Array = [];
            for (_loc_3 in this._lastDic)
            {
                
                _loc_5 = this._lastDic[_loc_3];
                if (_loc_5 + this.egnorTime <= param1)
                {
                    _loc_2.push(_loc_3);
                }
            }
            for each (_loc_4 in _loc_2)
            {
                
                delete this._lastDic[_loc_4];
            }
            return;
        }// end function

        public static function get instance() : TabKeyAI
        {
            if (_instance == null)
            {
                _instance = new TabKeyAI;
            }
            return _instance;
        }// end function

    }
}
